package com.wh.blog.controller;

import com.wh.blog.constant.Constants;
import com.wh.blog.mapper.UserMapper;
import com.wh.blog.model.BlogInfo;
import com.wh.blog.model.Result;
import com.wh.blog.model.UserInfo;
import com.wh.blog.service.BlogService;
import com.wh.blog.service.UserService;
import com.wh.blog.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    BlogService blogService;
    @Autowired
    UserService userService;

    @RequestMapping("/getList")
    public List<BlogInfo> getList() {
        return blogService.getBlogList();
    }

    @RequestMapping("/getAuthorInfoByBlogId")
    public UserInfo getAuthorInf(Integer blogId) {
        log.info("getAuthorInfoByBlogId的BlogId:{}", blogId);
        if (blogId <= 0) {
            return null;
        }
        BlogInfo blogInfo = blogService.selectById(blogId);
        return userService.selectById(blogInfo.getUserId());
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId, HttpServletRequest request) {
        log.info("getBlogDetail的BlogId:{}", blogId);
        if (blogId <= 0) {
            return null;
        }
        BlogInfo blogInfo = blogService.selectById(blogId);
        if (blogInfo == null) {
            return null;
        }
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        Integer userId = JwtUtils.getIdByToken(token);
        if (userId != null && userId == blogInfo.getUserId()) {
            blogInfo.setLoginUser(true);
        }
        return blogInfo;
    }

    @RequestMapping("/updateBlog")
    public Boolean updateBlog(BlogInfo blogInfo) {
        log.info("updateBlog的blogInfo:{}", blogInfo);
        if (blogInfo == null) {
            return false;
        }
        Integer count = blogService.updateBlog(blogInfo);
        if (count <= 0) {
            return false;
        }
        return true;
    }

    @RequestMapping("/delete")
    public Boolean deleteByBlogId(Integer blogId) {
        log.info("deleteByBlogId:{}", blogId);
        if (blogId <= 0) {
            return false;
        }
        Integer count = blogService.deleteByBlogId(blogId);
        if (count <= 0) {
            return false;
        }
        return true;
    }

    @RequestMapping("/addBlog")
    public Boolean addBlog(BlogInfo blogInfo, HttpServletRequest request) {
        log.info("addBlog:{}", blogInfo);
        if (blogInfo == null) {
            return false;
        }
        if (!StringUtils.hasLength(blogInfo.getTitle()) ||
                !StringUtils.hasLength(blogInfo.getContent())) {
            return false;
        }
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        Integer userId = JwtUtils.getIdByToken(token);
        if (userId == null) {
            return false;
        }
        blogInfo.setUserId(userId);
        Integer count = blogService.addBlog(blogInfo);
        if (count <= 0) {
            return false;
        }
        return true;
    }
}
